package org.example;

import org.example.db.ORMFramework;
import org.example.model.Person;
import org.example.annotation.MyAnnotation;

import java.lang.reflect.Field;

public class Main {
    @MyAnnotation(name = "tom")
    public void myMethod() {

    }

    public static void main(String[] args) throws IllegalAccessException {
        test1();
        test2();
    }

    private static void test2() {
        Person person = new Person(6, "小明", 12);
        try {
            Class<?> clazz = Class.forName("org.example.model.Person");
            Object obj = clazz.newInstance();
            Field id = clazz.getDeclaredField("id");
            Field name = clazz.getDeclaredField("name");
            Field age = clazz.getDeclaredField("age");
            id.setAccessible(true);
            name.setAccessible(true);
            age.setAccessible(true);
            id.set(obj,122);
            name.set(obj,"Vesper");
            age.set(obj,22);

            ORMFramework.save(obj);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private static void test1() {

        try {
            Class<?> clazz = Class.forName("org.example.Main");
            Object obj = clazz.newInstance();
            MyAnnotation annotation = clazz.getMethod("myMethod").getAnnotation(MyAnnotation.class);
            if (annotation != null) {
                System.out.println("name = " + annotation.name());
                System.out.println("hobby  = " + annotation.hobby());
                System.out.println("value = " + annotation.value());
            }
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}